#ifndef LIST_H
#define LIST_H

#include <cstdlib>
#include <easyx/graphics.h>
#include <iostream>
#define MAX_LEN 256
using namespace std;

/*
"*******************************************************"
"*                 COPYRIGHT STATEMENT                 *"
"*           THIS HEADER IS CODED BY ZEPHYRBD!         *"
"*This  header is open - sourced under the MIT License.*"
"*       Copyright 2018-2025 Minecraft ZBD Studio.     *"
"*******************************************************"
*/
struct List
{
	
	int id;
	char name[MAX_LEN];
	int sex,age,money;
	int college;
	char address[MAX_LEN];
	char tel[MAX_LEN];
	List *next;
};

bool isValidNumber(char* str)
{
	for (int i = 0; str[i] != '\0'; i++)
	{
		if (!isdigit(str[i])) return false;
	}
	return true;
}

void errorSolve(List *p)
{
	if(p==NULL) 
	{
		MessageBox(NULL,"内存分配失败，即将退出程序","警告",MB_ICONWARNING | MB_OK);
		exit(0);
	}
	return;
}

void insertList(List input,List *&head)
{
	List *p = (List *)malloc(sizeof(List));
	errorSolve(p);
	*p = input;
	p->next = NULL;
	if(head==NULL)
	{
		head = p;
		return;
	}
	if(p->id<head->id)
	{
		p->next = head;
		head = p;
		return;
	}
	List *tmp = head;
	while(tmp->next!=NULL&& tmp->next->id < p->id)
	{
		tmp = tmp->next;
	}
	p->next = tmp->next;
	tmp->next = p;
}

void printHead()
{
	system("cls");
	printf("%-10s %-20s %-10s %-10s %-10s %-20s %-10s %-11s\n",
		"ID", "Name", "Sex", "Age", "Money", "College", "Address", "Tel");
	string tmp(120, '-');
	printf("%s\n",tmp.c_str());
}

void printItem(List *p)
{
	if(p==NULL) return;
	char Name[MAX_LEN];
	char Address[MAX_LEN];
	
	strncpy(Name, p->name, 19);
	Name[19] = '\0';
	
	string colleges[] = {"","小学及以下","初中","高中或中技","本科或专科","研究生及以上"};
	
	strncpy(Address, p->address, 9);
	Address[9] = '\0';
	
	string sex = "男";
	if(!p->sex) sex = "女"; 
	printf("%-10d %-20s %-10s %-10d %-10d %-20s %-10s %-11s\n",
		p->id, Name, sex.c_str(), p->age,
		p->money, colleges[p->college].c_str(), Address, p->tel);
}

List *searchIDItem(List *head,int idx)
{
	if(head==NULL) return NULL;
	List *p = head;
	while(p!=NULL)
	{
		if(p->id==idx)
		{
			return p;
		}
		p=p->next;
	}
	return NULL;
}

void searchCollegeItem(List *head,int idx)
{
	if(head==NULL) return;
	int cnt = 0;
	printHead();
	List *p = head;
	while(p!=NULL)
	{
		if(p->college==idx)
		{
			printItem(p);
			cnt++;
		}
		p=p->next;
	}
	string output = "一共匹配到" + to_string(cnt) + "人！\n" + "详细信息在CMD窗口中！";
	MessageBox(NULL, output.c_str(), "提示", MB_ICONINFORMATION | MB_OK);
}

void deleteItem(List *&head,int idx)
{
	if(head == NULL) return;
	if(head->id==idx)
	{
		List *tmp = head;
		head = head->next;
		string output = "已经删除工号为" + to_string(tmp->id) + "的员工！";
		free(tmp);
		MessageBox(NULL, output.c_str(), "提示", MB_ICONINFORMATION | MB_OK);
		return;
	}
	List *p = head;
	while(p!=NULL)
	{
		List *tmp = p;
		p = p->next;
		if(tmp->id==idx)
		{
			string output = "已经删除工号为" + to_string(tmp->id) + "的员工！";
			free(tmp);
			MessageBox(NULL, output.c_str(), "提示", MB_ICONINFORMATION | MB_OK);
			return;
		}
	}
	MessageBox(NULL,"系统中查无此人！","错误",MB_ICONHAND | MB_OK);
}
#endif
